package com.nianzhen.student;

import java.util.ArrayList;

import com.lidroid.xutils.util.LogUtils;
import com.nianzhen.recruitment.BaseFragmentActivity;
import com.nianzhen.recruitment.R;
import com.nianzhen.recruitment.R.id;
import com.nianzhen.recruitment.R.layout;
import com.nianzhen.student.fra.Collect;
import com.nianzhen.student.fra.Job;
import com.nianzhen.student.fra.Userinfo;
import com.nianzhen.utils.BaseActivity;
import com.nianzhen.utils.ToastBreak;
import com.nianzhen.weight.CHSHRadioGroup;

import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.KeyEvent;
import android.view.Window;
import android.widget.RadioButton;

public class MainActivity extends BaseFragmentActivity {

	private ArrayList<Fragment> mFragments;
	private FragmentManager fragmentManager;
	private FragmentTransaction transaction;
	private CHSHRadioGroup rg;
	private RadioButton rbDongTai;
	private RadioButton rbGuangChang;
	private RadioButton rbMsg;
	private boolean isExit;

	@SuppressLint("NewApi")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.homepager);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		initFrag();
		initView();
		initRg();
	}

	@SuppressLint("NewApi")
	private void initRg() {
		// TODO Auto-generated method stub
		rg.setOnCheckedChangeListener(new CHSHRadioGroup.OnCheckedChangeListener() {

			public void onCheckedChanged(CHSHRadioGroup group, int checkedId) {
				// TODO Auto-generated method stub
				switch (checkedId) {
				case R.id.rbDongTai:
					changeRadio(0);
					break;
				case R.id.rbGuangChang:
					changeRadio(1);
					break;
				case R.id.rbMsg:
					changeRadio(2);
					break;
				default:
					break;
				}
			}

		});
	}

	private void initView() {
		// TODO Auto-generated method stub
		rg = (CHSHRadioGroup) findViewById(R.id.rg);
		rbDongTai = (RadioButton) findViewById(R.id.rbDongTai);
		rbGuangChang = (RadioButton) findViewById(R.id.rbGuangChang);
		rbMsg = (RadioButton) findViewById(R.id.rbMsg);
	}

	// 初始化frag
	private void initFrag() {
		// TODO Auto-generated method stub
		mFragments = new ArrayList<Fragment>();
		mFragments.add(new Job());
		mFragments.add(new Collect());
		mFragments.add(new Userinfo());
		changeRadio(0);
		LogUtils.d("mFragments init success");
	}

	private void changeRadio(int i) {
		fragmentManager = getSupportFragmentManager();
		transaction = fragmentManager.beginTransaction();
		transaction.replace(R.id.content, mFragments.get(i));
		transaction.commit();
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			exit();
			return false;
		} else {
			return super.onKeyDown(keyCode, event);
		}
	}

	public void exit() {
		if (!isExit) {
			isExit = true;
			ToastBreak.showToast("再按一次退出程序");
			mHandler.sendEmptyMessageDelayed(0, 2000);

		} else {
			Intent intent = new Intent(Intent.ACTION_MAIN);
			intent.addCategory(Intent.CATEGORY_HOME);
			startActivity(intent);
			System.exit(0);
		}
	}

	Handler mHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			super.handleMessage(msg);
			isExit = false;
		}
	};

}
